import { defineStore } from "pinia";
import { ref } from 'vue';
import {addApi, loadApi, updateEmployeeApi, deleteApi, deleteBatchApi} from '@/api/employee'
import { ElMessage, ElMessageBox } from 'element-plus';
import { departmentListApi } from "@/api/departmentList";
export const useEmployeeStore = defineStore('employee', () => {
//
  const pageNum = ref(1)
  const pageSize = ref(10)
  const name = ref('')
  const total = ref(0);
  const options = ref([])
  const formVisible = ref(false)
  const form = ref({})
  const ids = ref([])
  const formRef = ref()
  const departmentList = ref([])

  const getLoad = async () => {
      const res = await loadApi({
          pageNum: pageNum.value, 
          pageSize: pageSize.value,
          name: name.value
      })
      options.value = res.data.list
      total.value = res.data.total
      // console.log(options.value);
    //   console.log(res);
      const res2 =  await departmentListApi()
      departmentList.value = res2.data
    //   console.log(res2);
      
  }
  const reset = () => {
      name.value='';
      getLoad();
  }
  const handleAdd = () => {
      formVisible.value = true
      form.value = {}
  }
  const save = () => {
    form.value.id ? update() : add()
  }

  const add = async() => {
      const res = await addApi(form.value)
      if(res.code==='200') {
          ElMessage.success('操作成功')
          getLoad()
          formVisible.value=false
      } else {
          ElMessage.error(res.msg)
      }    
  }
  const update = async() => {
      const res = await updateEmployeeApi(form.value)
      if(res.code==='200') {
          ElMessage.success('操作成功')
          getLoad()
          formVisible.value=false
      } else {
          ElMessage.error(res.msg)
      }  
  }
  const handleUpdate = (row) => {
      form.value=JSON.parse(JSON.stringify(row))//深拷贝，不会影响行对象数据，用于编辑
      formVisible.value=true
  }
  const handleDelete = (id) => {
      ElMessageBox.confirm("删除数据后无法恢复,您确认吗?", "确认删除", { type: 'warning'}).then(async() => {
          const res = await deleteApi(id)
          if(res.code==='200') {
              ElMessage.success('操作成功')
              getLoad()
              formVisible.value=false
          } else {
              ElMessage.error(res.msg)
          }  
      }).catch(() => {
          ElMessage.info("已取消删除");
      })
      
  }
  const handleSelectionChange = (rows) => {
      // console.log(rows);
      ids.value = rows.map(row => row.id)
      // console.log(ids);
      
  }
  const deleteBatch = () => {
      if(!ids.value.length) {
          ElMessage.warning("请选择数据")
          return ;
      }
      ElMessageBox.confirm("删除数据后无法恢复,您确认吗?", "确认删除", { type: 'warning'}).then(async() => {
          const res = await deleteBatchApi(ids.value)
          if(res.code==='200') {
              ElMessage.success('操作成功')
              getLoad()
              formVisible.value=false
          } else {
              ElMessage.error(res.msg)
          }  
      }).catch(() => {
          ElMessage.info("已取消删除");
      })
  }
  const exportData = () => {
    
  }

  return {
    pageNum,
    pageSize,
    name,
    total,
    options,
    formVisible,
    form ,
    ids,
    departmentList,
    getLoad,
    reset,
    handleAdd,
    save,
    add,
    update,
    handleUpdate,
    handleDelete,
    handleSelectionChange,
    deleteBatch,
  }
})